import Vue from 'vue'
import Vuex from 'vuex'

import createPersistedState from 'vuex-persistedstate'

// import VuexPersistence from 'vuex-persist'

Vue.use(Vuex)

// const vuexLocal = new VuexPersistence({
//   storage: window.sessionStorage
// })

export default new Vuex.Store({
  plugins: [createPersistedState({
    //  存到sessionStorage
    //  传入的val是所有的store值
    key: 'liz',
    storage: window.sessionStorage,
    render (state) {
      // 要存储的数据：本项目采用es6扩展运算符的方式存储了state中所有的数据
      return { ...state }
    }
  })],
  state: {
    tabsList: [{
      path: '/user',
      name: '首页'
    }]
  },
  mutations: {
    selectMenu(state, val) {
      if (val.name === '首页') {} else {
        const result = state.tabsList.findIndex(item => item.name === val.name)
        // eslint-disable-next-line no-unused-expressions
        result === -1 ? state.tabsList.push(val) : ''
      }
      // sessionStorage.setItem('store', JSON.stringify(this.$store.state))
    },
    closeTab(state, val) {
      const result = state.tabsList.findIndex(item => item.name === val.name)
      state.tabsList.splice(result, 1)
    }
  },
  actions: {},
  modules: {}
})
